/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import org.aswing.ButtonGroup;
import org.aswing.Component;
import org.aswing.GridLayout;
import org.aswing.JCheckBox;
import org.aswing.JPanel;
import org.aswing.JRadioButton;

import com.bourre.data.collections.IndexedArray;

import fever.data.validators.ValidationResources;

import fvaswing.components.forms.FvFormField;

/**
 * Poll group ( using <strong>radiobutton</strong> ) as form field.
 * 
 * <p>Allow user to place a group of radiobutton as unique field into form.<br />
 * 
 * <p>Choices must be added to field before adding field into a form object.
 * 
 * <p>Example
 * {@code
 *   var pollRadio : FvRadioPollField = new FvRadioPollField( "Your opinion" );
 *   pollRadio.addChoice( "bad" );
 *   pollRadio.addChoice( "bad" );
 *   pollRadio.addChoice( "execellent" );
 * }
 *  
 * @author Romain Ecarnot
 */
class fvaswing.components.forms.FvRadioPollField extends FvFormField
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private var _minSelection : Number;
	private var _list : IndexedArray;
	private var _group : ButtonGroup;
	private var _rows : Number;
	private var _columns : Number;
	
	private var _listContainer : JPanel;
	private var _defaultSelection : JRadioButton;
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 * 
	 * @param Field's caption
	 */
	public function FvRadioPollField( label : String )
	{
		super( label );
		
		_group = new ButtonGroup();
		_list = new IndexedArray();
	}
	
	/**
	 * Defines row and column value for poll choices representation.
	 * 
	 * <p>If not defined, uses row == choices list length and column == 1.
	 */
	public function setRowAndColumn( rows : Number, columns : Number ) : Void
	{
		if( rows >= 0 ) _rows = rows;
		if( columns >= 0 ) _columns = columns;
	}
	
	/**
	 * Sets selected field value.
	 */
	public function setValue( choice : String ) : Void
	{
		super.setValue( choice );
	}
	
	/**
	 * Returns selected field value.
	 */
	public function getValue( ) : String
	{
		var l : Number = _listContainer.getComponentCount();
		
		for (var i : Number = 0; i < l; i++) 
		{
			var radio : JRadioButton = JRadioButton( _listContainer.getComponent( i ) );
			if( radio.isSelected() ) return radio.getText();
		}
		
		return null;
	}
	
	/**
	 * Adds new choice to poll.
	 * 
	 * <p>Choices must be added to field before adding field into a form object.
	 * 
	 * @param caption Choice's label
	 */
	public function addChoice( caption : String ) : Void
	{
		var radio : JRadioButton = _createRadioButton( caption );
		
		if( _list.length == 0 ) 
		{
			_defaultSelection = radio;
			radio.setSelected( true );
		}
		
		_group.append( radio );
		_list.push( radio );
	}
	
	/**
	 * Clears field value.
	 */
	public function clear() : Void
	{
		_defaultSelection.setSelected( true );
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	/**
	 * Returns component dedicated to field value.
	 */
	private function _createValueComponent() : Component
	{
		var l : Number = _list.length;
		var r : Number = ( _rows != undefined ) ? _rows : l;
		var c : Number = ( _columns != undefined ) ? _columns : 1;
		
		_listContainer = new JPanel( new GridLayout( r, c, 5, 5 ) );
		
		for (var i : Number = 0; i < l; i++) _listContainer.append( _list[ i ] );
		
		return _listContainer;
	}
	
	private function _createRadioButton( caption : String ) : JRadioButton
	{
		var radio = new JRadioButton( caption );
		radio.setHorizontalAlignment( JCheckBox.LEFT );
		radio.setHorizontalTextPosition( JCheckBox.RIGHT );
		
		return radio;
	}
	
	/**
	 * Sets selected field value.
	 */
	private function _initWithData( choice : String ) : Void
	{
		var l : Number = _listContainer.getComponentCount();
		
		for (var i : Number = 0; i < l; i++) 
		{
			var radio : JRadioButton = JRadioButton( _listContainer.getComponent( i ) );
			if( radio.getText() == choice ) radio.setSelected( true );
		}
	}
	
	/**
	 * Process validation
	 */
	private function _validateField() : Boolean
	{
		var l : Number = _listContainer.getComponentCount();
		
		for (var i : Number = 0; i < l; i++) 
		{
			var radio : JRadioButton = JRadioButton( _listContainer.getComponent( i ) );
			if( radio.isSelected() ) return _triggerResult( true );
		}
		
		return _triggerResult( false, ValidationResources.getInstance().required_field_error );
	}
}